package cn.geminis.everydata.service.entity;

import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.springframework.util.ObjectUtils;

import javax.persistence.*;
import java.util.UUID;

/**
 * @author puddi
 */
@Entity
@Getter
@Setter
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class Form {
    @Id
    @Column(length = 36)
    private String id;

    @Column(nullable = false)
    private String name;

    @Type(type = "json")
    @Column(columnDefinition = "json", nullable = false)
    private Object schemaData;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Category category;

    @PrePersist
    void createId() {
        if (ObjectUtils.isEmpty(this.id)) {
            this.id = UUID.randomUUID().toString();
        }
    }
}
